package com.fansmac.sc.controller;

import com.fansmac.sc.service.IDemoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;

import java.net.URI;


@RequestMapping("mix")
@RestController
public class MixedController {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AsyncRestTemplate asyncRestTemplate;

    @Autowired
    private IDemoApi iDemoApi;

    @RequestMapping(method = RequestMethod.GET)
    public String get() throws Exception {

        String result1 = restTemplate.getForObject("http://my-provider/test?content=RestTemplate", String.class);

        ListenableFuture<ResponseEntity<String>> future = asyncRestTemplate.getForEntity("http://my-provider/test?content=AsyncRestTemplate", String.class);

        HttpEntity entity = new RequestEntity(HttpMethod.POST, new URI("http://my-provider/test"));

        ListenableFuture<ResponseEntity<String>> future2 = asyncRestTemplate.postForEntity("http://my-provider/test", entity, String.class);

        String result3 = iDemoApi.echo("hello");


        return result1 + " " + future.get().getBody() + "  " + future2.get().getBody() + "  " + result3;
    }
}

